package com.intellij.ide.license.impl;

import com.intellij.ide.license.LicenseFactory;
import com.intellij.ide.license.LicenseMessages;
import com.intellij.ide.license.LicenseSource;
import com.intellij.licensecommon.data.LicenseData;

public abstract class AbstractLicenseSource implements LicenseSource {
    protected LicenseFactory licenseFactory;
    private LicenseData licenseData;

    public AbstractLicenseSource() {
    }

    public AbstractLicenseSource(LicenseFactory licenseFactory) {
        this.licenseFactory = licenseFactory;
    }

    @Override
    public void checkValid() throws LicenseException {
        LicenseData licenseData = getLicenseData();
        if (licenseData == null) {
            throw new LicenseException();
        }

        try {
            this.licenseFactory.createLicenseValidator().isValid(licenseData);
        } catch (LicenseException e) {
            setLicenseData(null);
            throw e;
        }
    }

    protected LicenseMessages getLicenseMessages() {
        return this.licenseFactory.getLicenseMessages();
    }

    @Override
    public LicenseData getLicenseData() {
        return this.licenseData;
    }

    public void setLicenseData(LicenseData licenseData) {
        this.licenseData = licenseData;
    }
}
